package com.itshidu.jiaowu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itshidu.jiaowu.dao.ClassesMapper;
import com.itshidu.jiaowu.dao.UserMapper;
import com.itshidu.jiaowu.entity.StudentClasses;
import com.itshidu.jiaowu.dao.StudentClassesMapper;
import com.itshidu.jiaowu.entity.User;
import com.itshidu.jiaowu.entity.Violate;
import com.itshidu.jiaowu.service.StudentClassesService;
import com.itshidu.jiaowu.util.CookieUtils;
import com.itshidu.jiaowu.util.Result;
import com.itshidu.jiaowu.vo.StudentClassesVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XiaZengming123
 * @since 2019-07-08
 */
@Service
@Primary
    public class StudentClassesServiceImpl extends ServiceImpl<StudentClassesMapper, StudentClasses> implements StudentClassesService {
    @Autowired UserMapper usermapper;
    @Autowired ClassesMapper classesmapper;
    @Autowired StudentClassesMapper studentClassesMapper;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    //给某个班级添加学生
    @Override
    public Result ClassStudentAdd(String name, String dateTime, HttpServletRequest request) {

        try {
            Date jointime = sdf.parse(dateTime);                                              //入班时间
            User user = usermapper.SelectByName(name);                                        //查出学生user
            String cookie = CookieUtils.getCookie(request, "classInfo");         //从cookie获取班级ID
            Long id = Long.valueOf(cookie);

            List<StudentClasses> list = studentClassesMapper.SelectStuclasByClasId(id);       //循环遍历判断学生是否已经在这个班
            boolean flog=false;
            for (StudentClasses li : list) {
                if (li.getStudentId().equals(user.getId())) {
                    flog=true;
                    break;
                }
            }
            if(flog){
                return Result.of(250, "该学生已存在");
            }
            StudentClasses sc = new StudentClasses();
            sc.setStudentId(user.getId());
            sc.setClassesId(id);
            sc.setJoinDate(jointime);
            sc.setOutDate(null);                                                //默认出班时间为空
            studentClassesMapper.insert(sc);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return Result.of(200, "添加成功");
    }


    @Override
    public Result selectS(Integer page,Integer limit,Long id) {
        PageHelper.startPage(page,limit);                                   //开始分页
        List<StudentClasses> students =studentClassesMapper.SelectByClassid(id);
        List<StudentClassesVo> list=new ArrayList();
        PageInfo<Violate> info = new PageInfo(list);
        for(StudentClasses s:students){
            Long Cid=s.getClassesId();
            Long Sid=s.getStudentId();
            String Cname=classesmapper.SelectNameById(Cid);
            String Sname=usermapper.SelectNameById(Sid);
            StudentClassesVo SCvo=new StudentClassesVo();
            SCvo.setId(s.getId());
            SCvo.setClasses(Cname);
            SCvo.setStudentname(Sname);
            SCvo.setRubandate(s.getJoinDate());
            SCvo.setLibandate(s.getOutDate());
            list.add(SCvo);
        }
        return Objects.requireNonNull(Result.of().put("code", 0))
                .put("msg", "")
                .put("count",info.getTotal())
                .put("data", list);
    }
}
